<?php
class Zendvn_System_Info{
	
	private $_ss;
	public function __construct(){
		$this->_ss = new Zend_Session_Namespace(APPLICATION_KEY);
	}
	public function setInfo($auth = null){
		if($auth != null){
			$this->setMemberInfo($auth->getIdentity()->id);
			$this->setGroupInfo($auth->getIdentity()->group_id);
			$this->setType();
			$this->setAclInfo();
		}
	}
	public function clearInfo(){
		$this->_ss->unsetAll();
	}
	public function getInfo($part = null){
		$info = $this->_ss->getIterator();
		if($part != null){
			return $info[$part];
		}
		return $info;
	}
	
	public function setType($value = 'Member'){
		$this->_ss->type = $value;
	}
	public function getType(){
		return $this->_ss->type;
	}
	public function setMemberInfo($member_id = null){
		if($member_id != null){
			$db = Zend_Db_Table::getDefaultAdapter();
			$select = $db->select()->from('users')->where('id = ?',$member_id,1);
			$result = $db->fetchRow($select);
			$this->_ss->member = $result;
		}
	}
	public function setGroupInfo($group_id = null){
		if($group_id != null){
			$db = Zend_Db_Table::getDefaultAdapter();
			$select = $db->select()->from('user_group')->where('id = ?',$group_id,1);
			$result = $db->fetchRow($select);
			$this->_ss->group = $result;
		}
	}
	public function getMemberInfo($part = null){
		if($part == null){
			return $this->_ss->member;
		}else {
			$info = $this->_ss->member;
			return $info[$part];
		}
	}
	public function getGroupInfo($part = null){
		if($part == null){
			return $this->_ss->group;
		}else {
			$info = $this->_ss->group;
			return $info[$part];
		}		
	}
	
	public function setAclInfo(){
		$acl = new Zend_Acl();
		
        $role = $this->getGroupInfo('name');
        $acl->addRole($role);

        $group_name = $this->getGroupInfo('name');

        $db = Zend_Db_Table::getDefaultAdapter();                
        $select = $db->select()->from('privileges as p')->where('p.group LIKE ?',$group_name);
        $result = $db->fetchAll($select);
		
        $allowAccess = array();
        $denyAccess = array();
        foreach ($result as $val){
            $area = $val['module'] . '-' . $val['controller'] . '-' . $val['action'];
            if($val['type'] == 0){
                $denyAccess[] = $area;
            }else{
                $allowAccess[] = $area;
            }
        }

        $acl->allow($role,null,$allowAccess);
        $acl->deny($role,null,$denyAccess);
		
		$this->_ss->acl = $acl;
	}
	
	public function getAclInfo(){    
		return $this->_ss->acl;
	}
}